// components/player/component-player.js
const innerAudioContext = wx.createInnerAudioContext();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    audios: {
      type: Array,
      value: []
    },
    curIndex: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    audio:{},
    isShow:false,
    status:'pause',
    lastIndex:-1
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _close:function () {
      this.setData({
        isShow:false
      });
      this._pause();
    },
    _next:function () {
      if (this.data.curIndex < this.data.audios.length-1) {
        this.data.curIndex++;
        this.play(this.data.curIndex);
      }
      
    },
    play: function (index) {
      var audio = this.data.audios[index]
      this.setData({
        isShow:true,
        audio:audio,
        curIndex:index,
        status:'play'
      });
      
      if (index != this.data.lastIndex) {
        innerAudioContext.src = audio['mp3_url'];
      }
      innerAudioContext.play();
      this.data.lastIndex = index;
    },
    stop:function(){
      innerAudioContext.stop();
    },
    _pause: function () {
      this.setData({
        status:'pause'
      });
      innerAudioContext.pause();
    },
    _play: function () {
      this.setData({
        status:'play'
      });
      this.play(this.data.curIndex);
    }
  },

  ready:function () {
    var audio = this.data.audios[this.data.curIndex];
    this.setData({
      audio: audio
    });
  }
})
